<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3" xmlns:head="http://www.w3.org/1999/html">
<head th:include="public/public_css::public_css ('出库复核')" />

<body style="background-color: #f2f2f2;">
<div class="layui-fluid larry-wrapper">
    <div class="layui-card">
        <div class="layui-row layui-col-space30">
        <div class="layui-col-xs24">
                <form class="layui-form layui-card-header  layui-card-header-auto" id="searchForm">
                    <div class="layui-inline">
                        <label class="layui-form-label">电子秤</label>
                        <select name="sx" id="sx" lay-filter="test">
                            <option value="192.168.30.89:11001">电子秤(192.168.30.89)</option>
                            <option value="192.168.30.18:11002">电子秤(192.168.30.18)</option>
                        </select>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">箱编号</label>
                        <input type="text" class="layui-input" name="xbh" id="xbh" placeholder="箱编号" />
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">实际重量</label>
                        <input type="text" class="layui-input" name="sjzl" id="sjzl" placeholder="实际重量" />
                    </div>
                </form>
            <!--顶部按钮展示区域 【 tpl-button 】 -->
            <div class="group-button tpl-button" id="toolbar"></div>
            <!--列表-->
            <section class="panel panel-padding layui-card-body">
                <div class="pro">
                    <label>散箱/集装箱：<span id="lx"></span></label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <label>联系人：<span id="lxr"></span></label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <label>地址：<span id="dz"></span></label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <label>重量：<span id="zl"></span></label>
                    <table id="table" class="table text-nowrap"></table>
                </div>
            </section>
        </div>
    </div>
    </div>
</div>

</body>


<div th:include="public/public_js::public_js"></div>
<div th:include="public/public_btn::public_btn"></div>
<style type="text/css">
</style>
<script th:inline="javascript">
    $(function () {
        var sx=$("#sx").val();
        var sjzl=$("#sjzl").val();
        var socket = new WebSocket('ws://' + sx + '/websocket');
        socket.onopen = function (event) {
            console.log('socket listening');
            socket.onmessage = function (event) {
                console.log('接收到消息' + event.data);
                sjzl= event.data;
                sjzlCz();
                console.log(14444414141);
            };
        };
        document.getElementById('xbh').focus();
    })
    var functions = [[${functions}]];
    layui.use(['list', 'layer', 'zzpUtils', 'jquery', 'laydate', 'tablebutton'], function () {
        var zzpUtils= layui.zzpUtils,
            form = layui.form,
            $ = layui.jquery,
            laydate = layui.laydate,
            tablebtn = layui.tablebutton;
        tablebtn.buildOnClick({
            type: 1,
            temp: $("#function_tpl"),
            functions: functions.headBtn,
            row: null,
            event: {
                searchEvent: function(){
                    zzpUtils.refreshTable("#table");
                },
                /*出库异常处理*/
                figureOut: function () {
                    figure();
                },
                /*出库异常处理*/
                printEvent: function (data) {
                    print(data);
                },
            }
        });
        //初始化
        zzpUtils.webSocket.init();
        //出库异常处理
        function figure(){
            var xbh=$("#xbh").val();
            zzpUtils.dialog({
                title: "出库异常处理",
                type: 2,
                content: "/Operation/getEdit?xbh="+xbh,
            });
        }
        //打印
        function print(data){
            var xbh=$("#xbh").val();
            var width=$("#sjzl").val();
            zzpUtils.confirm("您将打印快递面单", function (index) {
                zzpUtils.action({
                    url: "/wmsreview/print" ,
                    data: JSON.stringify({xbh:xbh,width:width}),
                    success: function (data) {
                        console.log('data==='+data.data[0]);
                        var LODOP=getLodop();
                        LODOP.PRINT_INIT('打印快递面单');
                        LODOP.ADD_PRINT_URL(0, 0, 746, '100%', data.data[0]);
                        LODOP.SET_PRINT_STYLEA(0, 'HOrient', 3);
                        setTimeout(function () {
                            LODOP.PREVIEW();
                        }, 500);
                    }, error: function (xhr, textStatus) {
                        console.log(textStatus);
                        console.log(xhr)
                    }
                })
            })
        }
        laydate.render({
            elem: '#test5'
            ,theme: '#20a0ff'
        })
        var $table = zzpUtils.table({
            elem: '#table',
            isApi: true,
            pagination:false,
            showColumns: false,
            url: '[{}]',
            columns: [{
                field: '',
                checkbox: true
            }, {
                field: 'hpbh',
                title: '货品编号'
            },{
                field: 'hpmc',
                title: '货品名称'
            },{
                field: 'sl',
                title: '数量'
            },{
                field: 'zl',
                title: '重量',
                sortable:true,
                sorter:percentSort
            }],
            queryParams: function () {//查询条件
                return  zzpUtils.getFormJson($("#searchForm"));
            },
            detailFormatter: function (index, row) {
                return "这里就没有确定过11";
            },

        });
        function percentSort(a, b) {
            var value_a = a.substr(0, a.length-1)
            var value_b = b.substr(0, b.length-1)
            return value_b-value_a;
            }
        form.on('select(test)', function(data){
            var sjzl=$("#sjzl").val();
            var socket = new WebSocket('ws://' + data.value + '/websocket');
            socket.onopen = function (event) {
                console.log('socket listening');
                socket.onmessage = function (event) {
                    console.log('接收到消息' + event.data);
                    sjzl= event.data;
                    sjzlCz();
                };
            };
        });
        $('#xbh').on('input propertychange', function(data) {
            var xbh=$("#xbh").val();
            console.log($("#sx").val())
            if(xbh.length==18){
                $.ajax({
                    type: "GET",
                    url: "/zxsswms/wmsreview/getXmx/"+xbh,
                    dataType: "JSON",
                    async: false,
                    success: function (data) {
                        console.log(666666,data.data.contentList);
                        if (data.type === 'success') {
                            layer.msg(data.title);
                        } else {
                            layer.msg(data.message);
                        }
                        $table.bootstrapTable('append', data.data.contentList);
                        $("#lxr").html(data.data.lxr);
                        $("#dz").html(data.data.dz);
                        $("#zl").html(data.data.zl);
                        if(data.data.type==0){
                            $("#lx").html("散箱");
                        }else{
                            $("#lx").html("集装箱");
                        }

                    },
                    error: function () { console.log("Error"); }
                });
            }else if(xbh.length!=18&&xbh!=""){
                layer.msg("箱编号输入有误，请重新输入！");
            }
        })

        function sjzlCz(){
            var xbh=$("#xbh").val();
            var sjzl=$("#sjzl").val();
            var llzl=$("#zl").html();
            var fyid="";
            var zl;
            var sl;
            var data = $table.bootstrapTable('getData');
            for(var i=0;i<data.length;i++){
                fyid=data[i].fyid;
                zl=data[0].zl;
                sl=data[0].sl;
            }
            var query=[{
                fyzt:2,
                fyid:fyid,
                sjzl:sjzl,
                xbh:xbh
            }];
            if(sjzl-llzl>zl/sl){
                $.ajax({
                    type: "POST",
                    url: "/zxsswms/wmsreview/add/",
                    dataType: "JSON",
                    async: false,
                    contentType: 'application/json',
                    data:JSON.stringify(query),
                    success: function (data) {
                        setTimeout(function () {
                            //top.layer.msg(data.title+data.message);
                        }, 500);
                        if(data.type!="success"){
                            zzpUtils.confirm("称重信息异常, 是否进行处理?", function (index) {
                                zzpUtils.dialog({
                                    title: "",
                                    type: 2,
                                    content: "/Operation/getEdit?xbh="+xbh,
                                });
                            })
                        }
                    },
                    error: function () { console.log("Error"); }
                });
            }else {
                if(sjzl!=""){
                    $.ajax({
                        type: "POST",
                        url: "/zxsswms/wmsreview/fhcg/",
                        dataType: "JSON",
                        async: false,
                        contentType: 'application/json',
                        data:JSON.stringify({xbh:xbh,sjzl:sjzl}),
                        success: function (data) {
                            if(data.message!=""){
                                layer.msg(data.message);
                            }else{
                                layer.msg(data.title);
                            }
                        },
                        error: function () { console.log("Error"); }
                    });
                }
            }
        }
        $('#sjzl').on('input propertychange', function(data) {
            sjzlCz();
        })
    });
</script>
</html>